export default {
  //存储数据
  state: {
    isCollapse: false,
    currentMenu: null,
    tabsList: [
      {
        path: '/',
        name: 'home',
        label: '首页',
        icon: 'home'
      }
    ]
  },
  //直接更新state的方法，不能写异步代码
  mutations: {
    collapseMenu(state) {
      state.isCollapse = !state.isCollapse;
    },
    //选择标签 选择面包屑
    selectMenu(state, val) {
      if (val.name === 'home') {
        state.currentMenu = null;
      } else {
        state.currentMenu = val;
        //如果等于-1说明tabsList不存在那么插入，否则什么都不做
        let result = state.tabsList.findIndex(item => item.name === val.name);
        result === -1 ? state.tabsList.push(val) : '';
        sessionStorage.setItem('tagList', JSON.stringify(state.tabsList));
      }
      // val.name === 'home' ? (state.currentMenu = null) : (state.currentMenu = val)
    },
    //关闭标签
    closeTab(state, val) {
      let result = state.tabsList.findIndex(item => item.name === val.name);
      state.tabsList.splice(result, 1);
      sessionStorage.setItem('tagList', JSON.stringify(state.tabsList));
    },
    //获取标签
    getTagList(state) {
      if (sessionStorage.getItem('tagList')) {
        let tagList = JSON.parse(sessionStorage.getItem('tagList'));
        state.tabsList = tagList;
      }
    },
    //清理标签
    clearTab(state, val) {
      //清除也一样 vuex和cookie都清除
      state.tabsList = [];
      sessionStorage.removeItem('tagList');
    }
  },
  //  commit()来触发mutation的调用 可以包含异步代码(定时器, ajax)
  actions: {
    // switchMenu({ state, commit, rootState }) {
    switchMenu() {
      this.commit('collapseMenu');
    }
  },
  getters: {
    getCollapse: function(state) {
      return state.isCollapse;
    }
  }
};
